package com.icinfo.cloud.provider.punish.common.documentoperate.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.punish.common.documentoperate.dto.CorrectionDocQueryDto;
import com.icinfo.cloud.provider.punish.common.documentoperate.service.ICaseCorrectionService;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.CorrectionDocQueryVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.vo.CorrectionDocTemporaryVo;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseDocumentRecord;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Desc 描述
 * @Author mahan
 * @Date 2023/2/23
 */
@Service
public class CaseCorrectionServiceImpl implements ICaseCorrectionService {


	@Autowired
	private CaseDocumentRecordMapper caseDocumentRecordMapper;
	@Resource
	IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;


	/**
	 * 描述：获取补正/更正文书信息目录编码
	 *
	 */
	@Override
	public Result<CorrectionDocQueryVo> getCorrectionDocCatalogCode(CorrectionDocQueryDto dto){
		CorrectionDocQueryVo reVo = new CorrectionDocQueryVo();
		Example example = new Example(CaseDocumentRecord.class);
		example.createCriteria().andEqualTo("caseId", dto.getCaseId())
				.andEqualTo("relationDocumentId", dto.getRelationDocumentRecordId())
				.andCondition("(document_sent_status = 'SEND_0' or document_record_status = '2')");
		int count = caseDocumentRecordMapper.selectCountByExample(example);
		if(count > Constant.INTEGER_0){
			return Result.success("当前文书存在未送达的补正/更正的文书，请处理后再试！", null);
		}
		//获取文书目录编码
		List<ConfigBusiParamDocumentInfo> spbList = configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(dto.getCaseId(), null, DocumentKindEnum.DOC_KIND_DK1BZGZSPB.getCode());
		if(ObjectUtil.isNotEmpty(spbList)){
			reVo.setCorrectionApproveDocCatalogCode(spbList.get(Constant.INTEGER_0).getDocumentCatalogCode());
		}
		List<ConfigBusiParamDocumentInfo> tzsList = configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(dto.getCaseId(), null, DocumentKindEnum.DOC_KIND_DK2BZGZTZS.getCode());
		if(ObjectUtil.isNotEmpty(tzsList)){
			reVo.setCorrectionDocCatalogCode(tzsList.get(Constant.INTEGER_0).getDocumentCatalogCode());
		}
		return Result.success(reVo);
	}


	/**
	 * 补正/更正暂存修改时，获取文书记录ID和编码
	 * @param dto
	 */
	@Override
	public CorrectionDocTemporaryVo getCorrectionDocTemporary(CorrectionDocQueryDto dto){
		CorrectionDocTemporaryVo reVo = new CorrectionDocTemporaryVo();
		//获取文书目录编码
		List<ConfigBusiParamDocumentInfo> spbList = configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(dto.getCaseId(), null, DocumentKindEnum.DOC_KIND_DK1BZGZSPB.getCode());
		if(ObjectUtil.isNotEmpty(spbList)){
			reVo.setCorrectionApproveDocCatalogCode(spbList.get(Constant.INTEGER_0).getDocumentCatalogCode());
		}
		List<ConfigBusiParamDocumentInfo> tzsList = configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(dto.getCaseId(), null, DocumentKindEnum.DOC_KIND_DK2BZGZTZS.getCode());
		if(ObjectUtil.isNotEmpty(tzsList)){
			reVo.setCorrectionDocCatalogCode(tzsList.get(Constant.INTEGER_0).getDocumentCatalogCode());
		}
		//根据关联的文书ID获取草稿记录信息
		Example example = new Example(CaseDocumentRecord.class);
		example.createCriteria().andEqualTo("caseId", dto.getCaseId())
				.andEqualTo("relationDocumentId", dto.getRelationDocumentRecordId())
				.andEqualTo("documentRecordStatus", "2");
		List<CaseDocumentRecord> list = caseDocumentRecordMapper.selectByExample(example);
		if(ObjectUtil.isNotEmpty(list)){
			for(CaseDocumentRecord record : list){
				if(reVo.getCorrectionApproveDocCatalogCode().equals(record.getDocumentCatalogCode())){
					reVo.setCorrectionApproveDocId(record.getId());
				}else if(reVo.getCorrectionDocCatalogCode().equals(record.getDocumentCatalogCode())){
					reVo.setCorrectionDocId(record.getId());
				}
			}
		}
		List<String> correctionDocCatalogCodeList = CollUtil.newArrayList();
		correctionDocCatalogCodeList.add(reVo.getCorrectionApproveDocCatalogCode());
		correctionDocCatalogCodeList.add(reVo.getCorrectionDocCatalogCode());
		reVo.setCorrectionDocCatalogCodeList(correctionDocCatalogCodeList);

		List<String> correctionDocIdList = CollUtil.newArrayList();
		correctionDocIdList.add(reVo.getCorrectionApproveDocId());
		correctionDocIdList.add(reVo.getCorrectionDocId());
		reVo.setCorrectionDocIdList(correctionDocIdList);
		return reVo;
	}

}
